package edu.tomee.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.tomee.db.DatabaseConnection;
import edu.tomee.db.MsgBox;

/**
 * Servlet implementation class DeleteMsg
 */
@WebServlet("/delMessage")

public class DeleteMsgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteMsgServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    DatabaseConnection db = new DatabaseConnection();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		HttpSession session = request.getSession();
		String[] selectedIds = request.getParameterValues("msgIds");
		System.out.println("selected ids "+ selectedIds.length);
		int[] delete = db.deleteMsgs(selectedIds);

		Integer uid = (Integer) session.getAttribute("userId");
		List<MsgBox> msgs = db.getMsgs(uid);
	    session.setAttribute("msgs", msgs);
	    if( null != msgs ){
	    	session.setAttribute("msgsCount", msgs.size());
	    }
		request.setAttribute("deltmsg", delete);
		PrintWriter pw = response.getWriter();
		pw.println("Record deleted successfully");
		//request.getRequestDispatcher("msgdel_rprt.jsp").include(request, response);
	}

}
